<?php

namespace App\Admin\Controllers;

use App\Models\Conf;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Cache;

class ConfController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('配置数据');
            $content->description('展示');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('配置数据');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('配置数据');
            $content->description('添加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Conf::class, function (Grid $grid) {
            //id, name, key, value, updated_at, created_at
            $grid->id('ID')->sortable();
            $grid->name('名称');
            $grid->key('键名');
            $grid->value('键值');
            $grid->created_at('created_at');
            $grid->updated_at('updated_at');
            $grid->actions(function ($actions){
                $actions->disableView();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Conf::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name','名称');
            $form->text('key','键名');
            $form->text('value','链接');
            $form->display('created_at','created_at');
            $form->display('updated_at','updated_at');
            $form->saved(function (Form $form) {
                //更新缓存
                if($form->key=='wisdom'){
                    Cache::forget('bannerWords');//banner上的文字
                }

            });

        });
    }
}
